#include "qpersonwidget.h"
#include "ui_qpersonwidget.h"
#include "qperson.h"

QPersonWidget::QPersonWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QPersonWidget)
{
    ui->setupUi(this);

    pa =new QPerson("pa");
    pb =new  QPerson("pb");

    pa->setProperty("age", 21);
    pb->setProperty("age", 30);

    ui->spinBox_pa_age->setValue(pa->property("age").toInt());
    ui->spinBox_pb_age->setValue(pb->property("age").toInt());

    connect(pa, &QPerson::ageChanged, this, &QPersonWidget::on_ageChanged);
    connect(pb, &QPerson::ageChanged, this, &QPersonWidget::on_ageChanged);

}

QPersonWidget::~QPersonWidget()
{
    delete ui;
}

void QPersonWidget::on_ageChanged(int arg1)
{
    QPerson *send = qobject_cast<QPerson *>(sender());

    ui->plainTextEdit->appendPlainText(send->property("name").toString() + QString::asprintf(", age: %d", arg1));

}

void QPersonWidget::on_pushButton_pa_age_inc_clicked()
{
    pa->incAge();
    ui->spinBox_pa_age->setValue(pa->age());
}

void QPersonWidget::on_spinBox_pa_age_valueChanged(int arg1)
{
    qDebug("spin click: %d\n", arg1);
    pa->setAge(arg1);
}

void QPersonWidget::on_pushButton_pb_age_inc_clicked()
{
    pb->incAge();
    ui->spinBox_pb_age->setValue(pb->age());
}

void QPersonWidget::on_spinBox_pb_age_valueChanged(int arg1)
{
    pb->setAge(arg1);
}

void QPersonWidget::on_pushButton_class_metainfo_clicked()
{

    const QMetaObject* meta = pa->metaObject();
    for(int i = meta->propertyOffset(); i< meta->propertyCount(); i++){
        // QMetaProperty prop = meta->property(i);
    }
}
